package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.ZyOwner;
import com.blb.zy_community.entity.ZyRepair;
import com.blb.zy_community.mapper.ZyOwnerMapper;
import com.blb.zy_community.service.IZyOwnerService;
import com.blb.zy_community.service.IZyRepairService;
import com.blb.zy_community.utils.JwtUtils;
import com.blb.zy_community.utils.RsaUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 报修信息 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController
public class ZyRepairController {

    @Autowired
    private IZyRepairService zyRepairService;

    @Autowired
    private IZyOwnerService zyOwnerService;

    @Autowired
    private ZyOwnerMapper zyOwnerMapper;

    @GetMapping("/zyRepair/page")
    public ResponseResult<Page<ZyRepair>> getZyRepairPage(
            @RequestParam(required = false, defaultValue = "1") Long current,
            @RequestParam(required = false, defaultValue = "5") Long size,
            @RequestParam(required = false) String repairState,
            @RequestParam(required = false) String ownerRealName,
            @RequestParam(required = false) String ownerPhoneNumber
    ) {
        Page<ZyRepair> zyRepairPage = zyRepairService.getZyRepairPage(new Page(current, size), repairState, ownerRealName, ownerPhoneNumber);
        return ResponseResult.ok(zyRepairPage);
    }

    /**
     * 添加维修信息
     * @param zyRepair
     * @return
     */
    @PostMapping("/zyRepair")
    public ResponseResult<ZyRepair> addZyRepair(@RequestBody ZyRepair zyRepair){
        zyRepair.setCreateTime(LocalDateTime.now());
        zyRepair.setRepairState("Pending");
        ZyOwner zyOwner = zyOwnerService.getOne(new QueryWrapper<ZyOwner>().eq("owner_real_name", zyRepair.getCreateBy()));
        zyRepair.setUserId(zyOwner.getOwnerId());
        zyRepair.setRepairNum("BX_"+new Random().nextInt());
        zyRepairService.save(zyRepair);
        return ResponseResult.ok(zyRepair);
    }

    /**
     * 根据id查询报修信息
     * @param request
     * @return
     */
    @GetMapping("zyRepairList")
    public ResponseResult<String> selectZyRepairByUserId(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        String createUser = JwtUtils.getUsernameFromToken(token, RsaUtils.publicKey);
        ZyOwner owner = zyOwnerMapper.selectOne(new QueryWrapper<ZyOwner>().eq("owner_real_name", createUser));
        List<ZyRepair> zyRepairs = zyRepairService.selectZyRepairByUserId(owner.getOwnerId());
        return ResponseResult.ok(zyRepairs);
    }

}
